package io.zcoder.innoway.adapters;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import io.zcoder.innoway.R;
import io.zcoder.innoway.utils.Utils;
import io.zcoder.innoway.models.EntryTime;
import io.zcoder.innoway.widgets.TextViewCustom;


public class HistoryEntryTimeAdapter extends RecyclerView.Adapter<HistoryEntryTimeAdapter.EntryPlaceHolder> {

    private List<EntryTime> mEntryTimes;
    private String mUnit;
    private EntryCallBack mCallBack;

    public HistoryEntryTimeAdapter(EntryCallBack callBack, List<EntryTime> entryTimes, String unit) {
        mEntryTimes = new ArrayList<>();
        int year = Integer.MAX_VALUE;
        for (int i = entryTimes.size() - 1; i >= 0; i--) {
            EntryTime entryTime = entryTimes.get(i);
            Date date = Utils.convertToDate(entryTime.getTime(), Utils.DATE_FORMAT_YYYY_MM);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            if (calendar.get(Calendar.YEAR) < year) {
                year = calendar.get(Calendar.YEAR);
                entryTime.setYear(year);
            }
            mEntryTimes.add(entryTime);
        }
        mCallBack = callBack;
        mUnit = unit;
    }

    public class EntryPlaceHolder extends RecyclerView.ViewHolder {
        TextViewCustom mYear, mTime, mValue;
        LinearLayout mLayoutYear, mLayoutItem;

        public EntryPlaceHolder(View itemView) {
            super(itemView);
            mLayoutYear = (LinearLayout) itemView.findViewById(R.id.layout_year);
            mLayoutItem = (LinearLayout) itemView.findViewById(R.id.layout_item);
            mYear = (TextViewCustom) itemView.findViewById(R.id.year);
            mTime = (TextViewCustom) itemView.findViewById(R.id.time_text);
            mValue = (TextViewCustom) itemView.findViewById(R.id.value);
        }
    }


    @Override
    public EntryPlaceHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_history_trend, parent, false);

        return new EntryPlaceHolder(itemView);
    }

    @Override
    public void onBindViewHolder(EntryPlaceHolder holder, final int position) {
        EntryTime entryTime = mEntryTimes.get(position);
        holder.mTime.setText(Utils.convertToString(Utils.convertToDate(entryTime.getTime(),
                Utils.DATE_FORMAT_YYYY_MM), Utils.DATE_FORMAT_MMMM_YYYY));
        holder.mValue.setText(entryTime.getValueWithUnit(mUnit));
        if (entryTime.getYear() > 0) {
            holder.mLayoutYear.setVisibility(View.VISIBLE);
            holder.mYear.setText(entryTime.getYear() + "");
        } else
            holder.mLayoutYear.setVisibility(View.VISIBLE);
        holder.mLayoutItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCallBack != null) {
                    mCallBack.onSelect(position);
                }
            }
        });
    }


    @Override
    public int getItemCount() {
        return mEntryTimes == null ? 0 : mEntryTimes.size();
    }

    public interface EntryCallBack {
        public void onSelect(int position);
    }

}
